﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using EasyLucky.Common;
using EasyLucky.Dal.Dal;
using EasyLucky.Dal.Entity;
using EasyLucky.Manager.Models;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace EasyLucky.Manager.ViewModels
{
    public class LuckyManagerViewModel:ObservableObject
    {
        private LuckyManagerDataM dataM;

        public LuckyManagerDataM DataM
        {
            get { return dataM; }
            set { SetProperty(ref dataM, value); }
        }

        private GiftDal giftDal;

        private LuckyDal luckyDal;

        public LuckyManagerViewModel()
        {
            this.DataM = new LuckyManagerDataM()
            {
                Luckys = new System.Collections.ObjectModel.ObservableCollection<Common.Lucky>(),
                NewLucky = new Common.Lucky() { },
                NewVisibility = System.Windows.Visibility.Collapsed,
            };
        }

        private void InitInfo()
        {
            this.DataM.Luckys.Clear();
            var entities = this.luckyDal.QueryAll();
            foreach (var entity in entities)
            {
                var lucky = new Lucky()
                {
                    Id = entity.Id,
                    Name = entity.Name,
                    Count = entity.Count,
                    GiftId = entity.GiftId,
                    Level = entity.Level,
                    LuckyOrder = entity.LuckyOrder,
                    LuckyType = entity.LuckyType,
                };
                this.DataM.Luckys.Add(lucky);
            }
        }

        private void InitGiftInfo()
        {
            var giftEntitys = this.giftDal.QueryAll();
            var gifts = new List<Gift>();
            foreach(var giftEntity in giftEntitys)
            {
                var gift = new Gift()
                {
                    Name = giftEntity.Name,
                    Id = giftEntity.Id,
                    ImageUrl = giftEntity.ImageUrl,
                };
                gifts.Add(gift);
            }
            this.DataM.Gifts = new System.Collections.ObjectModel.ObservableCollection<Gift>(gifts);
        }

        #region Loaded

        private IRelayCommand loadedCommand;

        public IRelayCommand LoadedCommand
        {
            get { return loadedCommand ??= new RelayCommand(Loaded); }
        }

        private void Loaded()
        {
            if (giftDal == null)
            {
                this.giftDal = new GiftDal();
                InitGiftInfo();
            }
            if (this.luckyDal == null)
            {
                this.luckyDal = new LuckyDal();
                this.InitInfo();
            }
        }
        #endregion

        #region 查询

        private IRelayCommand queryCommand;

        public IRelayCommand QueryCommand
        {
            get { return queryCommand ??= new RelayCommand(Query); }
        }

        private void Query()
        {
            this.InitInfo();
        }

        #endregion

        #region 新增

        private IRelayCommand newCommand;

        public IRelayCommand NewCommand
        {
            get { return newCommand ??= new RelayCommand(New); }
        }

        private void New()
        {
            this.DataM.NewVisibility = System.Windows.Visibility.Visible;
            this.DataM.NewLucky = new Lucky();
        }


        private IRelayCommand saveCommand;

        public IRelayCommand SaveCommand
        {
            get { return saveCommand ??= new RelayCommand(Save); }
        }

        private void Save()
        {
            if (string.IsNullOrEmpty(this.DataM.NewLucky.Name))
            {
                return;
            }
            var lucky = new LuckyEntity()
            {
                Id = this.DataM.NewLucky.Id,
                Name = this.DataM.NewLucky.Name,
                Count = this.DataM.NewLucky.Count,
                GiftId = this.DataM.NewLucky.GiftId,
                Level = this.DataM.NewLucky.Level,
                LuckyOrder = this.DataM.NewLucky.LuckyOrder,
                LuckyType = this.DataM.NewLucky.LuckyType

            };
            bool flag = false;
            if (lucky.Id > 0)
            {
                int cnt = this.luckyDal.UpdateLucky(lucky);
                if (cnt > 0)
                {
                    flag = true;
                }
            }
            else
            {
                int id = this.luckyDal.AddLucky(lucky);
                if (id > 0)
                {
                    flag = true;

                }
            }
            if (flag)
            {
                this.InitInfo();
            }
        }

        #endregion

        #region 编辑

        private IRelayCommand editCommand;

        public IRelayCommand EditCommand
        {
            get { return editCommand ??= new RelayCommand<Lucky>(Edit); }
        }

        private void Edit(Lucky lucky)
        {
            if (lucky == null)
            {
                return;
            }
            this.DataM.NewVisibility = Visibility.Visible;
            this.DataM.NewLucky = lucky;
        }

        #endregion

        #region 删除

        private IRelayCommand delCommand;

        public IRelayCommand DelCommand
        {
            get { return delCommand ??= new RelayCommand<Lucky>(Del); }
        }

        private void Del(Lucky lucky)
        {
            if (lucky == null)
            {
                return;
            }
            if (lucky.Id < 1)
            {
                return;
            }
            var entity = new LuckyEntity()
            {
                Id = lucky.Id,
                Name = lucky.Name,
                Count = lucky.Count,
                GiftId = lucky.GiftId,
                Level = lucky.Level,
                LuckyOrder = lucky.LuckyOrder,
                LuckyType = lucky.LuckyType,
            };
            int cnt = this.luckyDal.DeleteLucky(entity);
            if (cnt > 0)
            {
                InitInfo();
            }
        }

        #endregion
    }
}
